﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.WorkFlow.Template;
using HF.BaseWebCtrlLib;
using HF.WorkFlow.Engine;
using HF.Framework;
using HF.Framework.Web;


public partial class BasePages_AccreditPage_AccreditWorkflow : System.Web.UI.Page
{
    string UserId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        UserId = this.Page.User.Identity.Name;
        BaseUserPage.LoadWaitPage();
        if (!this.Page.IsPostBack)
        {
            LoadLeftTree("####", UserId, tvWorkflow.Nodes);
            bind();  
        }
       

    }
    private void bind()
    {
        DataTable acDt = AccreditUser.GetInstance().GetToAccreditTable(UserId);
        gvAccredit.DataSource = acDt;
        gvAccredit.DataBind();

        acDt = AccreditUser.GetInstance().GetHaveAccreditTable(UserId);
        gvHaveAccredit.DataSource = acDt;
        gvHaveAccredit.DataBind();

    }
    /// <summary>
    /// 递归装载任务导航
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    public void LoadLeftTree(string key, string userId, TreeNodeCollection startNodes)
    {
        try
        {
            if (key == null || key == "") key = "####";
            DataTable table = WorkFlowClass.GetInstance().GetChildClass(key);
            foreach (DataRow row in table.Rows)
            {
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["Caption"].ToString();
                tmpNode.Target = "_self";
                //tmpNode.NavigateUrl = @"AssignAccredit.aspx?WorkflowId";
                tmpNode.ImageUrl = @"..\..\images\tree_27.gif";
                tmpNode.NavigateUrl = "#";
                tmpNode.Value = row["WfclassId"].ToString();
                startNodes.Add(tmpNode);
                LoadWorkflow(tmpNode.Value, tmpNode.ChildNodes);
                LoadLeftTree(tmpNode.Value, userId, tmpNode.ChildNodes);
            }
        }
        catch (Exception ex)
        {
            throw new Exception("LoadLeftTree函数," + ex.Message);
        }

    }
    public void LoadWorkflow(string key, TreeNodeCollection startNodes)
    {
        DataTable table = WorkFlowTemplate.GetInstance().GetWorkFlowsByClassId(key);
        foreach (DataRow row in table.Rows)
        {
            TreeNode tmpNode = new TreeNode();
            tmpNode.Text = row["FlowCaption"].ToString();
            //tmpNode.Target = "usersfrm";
            //tmpNode.NavigateUrl = @"AssignAccredit.aspx?WorkflowId=" + row["WorkFlowId"].ToString() +
            //                      "&WorkTaskId=" + row["WorkTaskId"].ToString() + "&UserId=" + UserId;
            tmpNode.ImageUrl = @"..\..\images\workflow.ico";
            tmpNode.NavigateUrl = "#";
            tmpNode.Value = row["WorkFlowId"].ToString();
            startNodes.Add(tmpNode);
            LoadWorkTask(tmpNode.Value, tmpNode.ChildNodes);
        }
    }
    public void LoadWorkTask(string key, TreeNodeCollection startNodes)
    {
        DataTable table = WorkTask.GetInstance().GetWorkTasks(key);
        string tasktypeId = "";
        foreach (DataRow row in table.Rows)
        {
            tasktypeId=row["tasktypeid"].ToString();
            if (tasktypeId == "4" || tasktypeId == "6") continue;//控制节点和子流程节点
            if (AccreditUser.GetInstance().IsTaskOperator(UserId, key, row["WorkTaskId"].ToString()))
            {
                TreeNode tmpNode = new TreeNode();
                tmpNode.Text = row["TaskCaption"].ToString();
                tmpNode.ImageUrl = @"..\..\images\worktask.ico";
                tmpNode.NavigateUrl = "#";
                tmpNode.Value = row["WorkTaskId"].ToString();
                tmpNode.ShowCheckBox = true;
                startNodes.Add(tmpNode);
            }
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (tvWorkflow.CheckedNodes.Count<=0)
        {
            WorkDialog.ShowMessage(this,"请先选择授权的任务!");
            return;

        }
        if (txtUsers.Text.Length <= 0)
        {
            WorkDialog.ShowMessage(this, "请先选择授权的用户!");
            return;
        }
        
        string[] users = txtUsers.Text.Split(',');
        WorkFlowRuntime wr = new WorkFlowRuntime();
        foreach (TreeNode tn in tvWorkflow.CheckedNodes)
        {
            foreach (string touser in users)
            {
                if (UserId == touser.Trim()) continue;//自己给自己授权无效

                wr.AssignAccredit(UserId, touser.Trim(), tn.Parent.Value, tn.Value);

            }
        }
        bind();
        Label1.Text= "授权成功!";
    }

   
    protected void btnTackAccredit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in gvAccredit.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
            if (ch.Checked)
            {
                
                AccreditUser.GetInstance().RemoveMoveAccredit(gvAccredit.DataKeys[gvr.DataItemIndex].Value.ToString());
                
            }
        } 
        
        bind();


    }
}
